package com.china08.yxyapi.api.manage.common;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSettingDomains;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingDomainsRepository;
import com.china08.yxyapi.security.profile.YxyProfile;

@CrossOrigin
@RestController
public class ShowInToWebController extends YxyApiBaseController {

    @Autowired
    SwsSettingDomainsRepository swsSettingDomainsRepository;

    @RequestMapping(value = { "/api_yw/manage/common/show/into/web", "/api_yw/manage/tec/teaching/show/into/web" }, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response) {

        YxyProfile yxyProfile = getProfile(request, response);

        String schoolId = yxyProfile.getSchoolId();

        List<SwsSettingDomains> swsSettingDomains = swsSettingDomainsRepository.findBySchoolId(schoolId);

        RespModel respModel = new RespModel();
        if (swsSettingDomains.size() > 0) {
            SwsSettingDomains swsSettingDomain = swsSettingDomains.get(0);
            String domainName = swsSettingDomain.getDomainName();
            respModel.hasDomain = StringUtils.isNotBlank(domainName);
            respModel.domainName = domainName;
        }
        return respModel;
    }

    protected static class ReqModel extends YxyApiPageReqModel {

    }

    protected static class RespModel extends YxyApiRespModel {
        public boolean hasDomain = false;
        public String domainName;
    }
}
